package net.appportal.ant.tasks;

import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import ca.beq.util.win32.registry.RegistryException;
import ca.beq.util.win32.registry.RegistryKey;
import ca.beq.util.win32.registry.RegistryValue;
import ca.beq.util.win32.registry.RootKey;

public class RegistryTask extends Task
{
	private final String TASK_NAME= "registry"; 
	private Vector tasks = new Vector();
	private boolean verbose = false;
	private boolean failonerror = true;
	
	private String property = "";
	private String key = "";
	private String name = "";
	private String rootkey = "";
	
	/**
	 * 
	 * @param failonerror
	 */
	public void setFailonerror( boolean failonerror ) 
	{
		this.failonerror = failonerror;
	}	
	
	/**
	 * 
	 * @param property
	 */
	public void setProperty( String property ) 
	{
		this.property = property;
	}	
	
	/**
	 * 
	 * @param verbose
	 */
	public void setVerbose( boolean verbose )
	{
		this.verbose = verbose;
	}
	
	/**
	 * 
	 * @param key
	 */
	public void setKey( String key )
	{
		this.key = key;
	}
	
	/**
	 * 
	 * @param key
	 */
	public void setName( String name )
	{
		this.name = name;
	}
	
	/**
	 * 
	 * @param rootkey
	 */
	public void setRootkey( String rootkey )
	{
		this.rootkey = rootkey;
	}
	
	/**
	 * 
	 * @throws  BuildException
	 */
	public void execute() throws BuildException
	{
		try 
		{
			if ( rootkey == null || rootkey.equals( "" ) )
				throw new BuildException( "The 'rootkey' attribute is required with '"+ TASK_NAME+"'." );		
			
			if ( property == null || property.equals( "" ) ) 
				throw new BuildException( "The 'property' attribute is required with '"+ TASK_NAME+"'." );
			
			if ( name == null || name.equals( "" ) ) 
				throw new BuildException( "The 'name' attribute is required with '"+ TASK_NAME+"'." );	
			
			String value = "";
			RegistryKey r = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE, name);
			if (verbose)
			{
				log("rootkey : " + rootkey);
				log("name : " + name);
				log("key : " + key);
				log("property : " + property);
			}
			if(r.hasValue(key))   
			{  
			   RegistryValue v = r.getValue(key);  
			   value = v.toString();  
//			   log("value : " + value);
			   value = value.replaceAll(":REG_SZ:", "").replaceAll(":REG_DWORD:", "");
//			   value = value.replaceAll(v.getName()+":", "").replaceAll("REG_SZ:", "").replaceAll("REG_DWORD:", "");
//			   log("value : " + value);
//			   System.out.println("lineage2 working dir : " + value);  
			}  
			else
			{
			    System.out.println("Not Existed Key");
			}

//			log("Set property: " + property + " -> " + propertyValue, Project.MSG_VERBOSE);
			if (verbose)
			{
				log("value : " + value);
			}

			getProject().setUserProperty(property,value);
		} 
		catch (Exception e) 
		{
			if (e instanceof RegistryException) 
			{
				if (failonerror)
				{
					throw new BuildException(e);
				}
			}
			else
			{
				throw new BuildException(e);
			}
		}
	}

	public void addTask( Task task ) 
	{
	      tasks.addElement( task );		
	}
}